package com.itcast.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.reggie.pojo.Employee;
import com.itcast.reggie.pojo.R;
import com.itcast.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/employee")
public class ReggieController{
    @Autowired
    private EmployeeService employeeService;

    //登录验证
    @PostMapping("/login")
    R<Employee> login(@RequestBody Employee employee, HttpSession session) {

        //将页面得到的密码进行md5加密
        String password = employee.getPassword();
        String Md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名查询用户信息
        String username = employee.getUsername();
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Employee ruquest = employeeService.getOne(wrapper);
        //如果没有查到.返回失败结果
        if (ruquest == null) {
            return R.error("登录失败");
        }

        //密码比对
        if (!ruquest.getPassword().equals(Md5Password)) {
            return R.error("密码错误");
        }
        //查看员工状态
        if (ruquest.getStatus() == 0) {
            return R.error("员工已禁用");
        }
        //登录成功,存入session
        session.setAttribute("employee", ruquest.getId());
        return R.success(ruquest);
    }

    //退出主页
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //添加员工
    @PostMapping
    public R<String> saveEmployee(HttpServletRequest request, @RequestBody Employee employee) {

        log.info("新增员工，员工信息：{}", employee.toString());
        //设置初始密码,加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
        Long empid = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empid);
        //employee.setUpdateUser(empid);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //根据姓名查询
    @GetMapping("/page")
    public R<Page> GetByPage(int page, int pageSize, String name) {
        Page page1 = new Page(page, pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), "name", name);
        wrapper.orderByAsc("create_time");
        Page page2 = employeeService.page(page1, wrapper);
        return R.success(page2);
    }

    //修改状态
    @PutMapping
    public R<String> updateStatus(HttpServletRequest request, @RequestBody Employee employee) {
        //获取当前线程的id
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}", id);

        log.info(employee.toString());
        Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    //修改表格数据回写
    @GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable Long id) {
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }




}


